package com.fc.test.controller.admin;

import com.fc.test.common.file.FileUploadUtils;
import com.fc.test.common.file.FileUtils;
import com.fc.test.model.auto.*;
import com.fc.test.service.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import com.fc.test.common.domain.AjaxResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.net.URLEncoder;

import static org.springframework.http.MediaType.APPLICATION_PDF_VALUE;

/**
 * 该接口为了前后端分离or手机端服务不用登录的接口  访问地址:localhot:8080/ApiController/test
 * 如何还需要其他 接口不登陆就能访问：ShiroFilterMapFactory.java里面配置开放自己的controller
 *
 * @author fuce
 * @ClassName: ApiController
 * @date 2020-04-15 22:59
 */
@Api(value = "接口API", tags = "接口API")
@Controller
@RequestMapping("/ApiController")
public class ApiController {


    @Resource
    private ItemListService itemListService;

    @Resource
    private AthingService athingService;

    @Resource
    private HandleCaseService handleCaseService;

    @Autowired
    private HandleDatailService handleDatailService;

    @Autowired
    private PolicyService policyService;

    @Autowired
    private HhbFileService hhbFileService;

    //@Log(title = "${TsysTables.tableComment}集合查询", action = "111")
    @ApiOperation(value = "测试方法", notes = "测试方法")
    @GetMapping("/test")
    @ResponseBody
    public AjaxResult test() {

        return AjaxResult.success();
    }


    @ApiOperation(value = "一件事清单列表接口", notes = "一件事清单列表接口")
    @GetMapping("/athing")
    @ResponseBody
    public AjaxResult athing(@RequestParam(defaultValue = "5", name = "pageSize", required = false) int pageSize,
                             @RequestParam(defaultValue = "1", name = "pageNum", required = false) int pageNum,
                             @RequestParam(name = "searchText", required = false) String searchText) {
        PageInfo<Athing> itemListPageInfo = athingService.list2(pageNum, pageSize, searchText);
        return AjaxResult.successData(200, itemListPageInfo);

    }

    @ApiOperation(value = "办事指南列表接口", notes = "办事指南列表接口")
    @GetMapping("/guide")
    @ResponseBody
    public AjaxResult guide(@RequestParam(defaultValue = "5", name = "pageSize", required = false) int pageSize,
                            @RequestParam(defaultValue = "1", name = "pageNum", required = false) int pageNum,
                            @RequestParam(name = "searchText", required = false) String searchText) {
        PageInfo<ItemList> itemListPageInfo = itemListService.list2(pageNum, pageSize, searchText);
        return AjaxResult.successData(200, itemListPageInfo);

    }

    @ApiOperation(value = "四办列表接口", notes = "四办列表接口")
    @GetMapping("/handle")
    @ResponseBody
    public AjaxResult handle(@RequestParam(defaultValue = "5", name = "pageSize", required = false) int pageSize,
                             @RequestParam(defaultValue = "1", name = "pageNum", required = false) int pageNum,
                             @RequestParam(name = "searchText", required = false) String searchText) {
        PageInfo<HandleCase> handleCasePageInfo = handleCaseService.list2(pageNum, pageSize, searchText);
        return AjaxResult.successData(200, handleCasePageInfo);

    }


    @ApiOperation(value = "赋权要素清单接口", notes = "赋权要素清单接口")
    @GetMapping("/handle/detail")
    @ResponseBody
    public AjaxResult handle(@ApiParam(name = "id", value = "参数id通过四办列表接口返回的结果集中获取", required = true)
                             @RequestParam(name = "id", required = true) String id) {
        HandleDatail handleDatail = handleDatailService.selectByPrimaryKey(id);
        return AjaxResult.successData(200, handleDatail);

    }


    @ApiOperation(value = "惠民政策接口", notes = "惠民政策接口")
    @GetMapping("/policy")
    @ResponseBody
    public AjaxResult policy(@RequestParam(defaultValue = "5", name = "pageSize", required = false) int pageSize,
                             @RequestParam(defaultValue = "1", name = "pageNum", required = false) int pageNum,
                             @RequestParam(name = "searchText", required = false) String searchText) {
        PageInfo<Policy> handleCasePageInfo = policyService.list2(pageNum, pageSize, searchText);
        return AjaxResult.successData(200, handleCasePageInfo);

    }

    @ApiOperation(value = "下载红黑榜", notes = "下载红黑榜")
    @GetMapping("/downloadHHB")
    public void downloadHHB(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String fileName = "红黑榜.pdf";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String filePath = hhbFileService.queryStatusON();
//        File file = new File(filePath);
        File file  = ResourceUtils.getFile(filePath);
        FileInputStream fis = new FileInputStream(file);
//        this.getClass().getClassLoader().getResourceAsStream(filePath);
        response.reset();
        ServletOutputStream out = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentType("application/octet-stream;charset=UTF-8");
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096);
        byte[] cache = new byte[4096];
        for (int offset = fis.read(cache); offset != -1; offset = fis.read(cache)) {
            byteOutputStream.write(cache, 0, offset);
        }
        byte[] bt = byteOutputStream.toByteArray();

        out.write(bt);

        out.flush();
        out.close();
        fis.close();


    }

}
